package com.example.shop.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.shop.domain.Agreement;
import com.example.shop.domain.Goods;
import com.example.shop.domain.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface AgreementMapper extends BaseMapper<Agreement> {


    List<Agreement> select(Integer id);

    @Delete({
            "DELETE FROM t_agreement",
            "WHERE id=#{id} AND user_in=#{userId}"
    })
    int deleteByGoodsId(Integer id, Integer userId);


    //    @Select("SELECT ag.*,gs.* FROM t_agreement ag,t_goods gs WHERE ag.user_in = #{userId} AND gs.id=ag.goods_id")

    @Select({"SELECT a.*, ",
            "g.id as goods_id, ",
            "g.name as name, ",
            "g.price as price, ",
            "g.unit as unit, ",
            "g.img as img, ",
            "g.remark as remark ",
            "FROM t_agreement a ",
            "LEFT JOIN t_goods g ON a.goods_id = g.id ",
            "WHERE a.user_in = #{userId}"})
    List<Agreement> selectListIn(@Param("userId") Integer userId);

    //    @Select({"UPDATE t_agreement",
//            "SET status =#{agreement.status} ",
//            "WHERE user_out = #{agreement.userOut} AND goods_id=#{agreement.goodsId } AND status=0"
//    })
//    List<Agreement> updateByAgreement(@Param("agreement") Agreement agreement);
//
//
    @Select({
            "<script>",
            "UPDATE t_agreement",
            "SET status =#{agreement.status} ",
            "WHERE user_out = #{agreement.userOut} AND goods_id=#{agreement.goodsId }",
            "<if test='agreement.status == 1'>",
            "AND status=0",
            "</if>",
            "<if test='agreement.status == 2'>",
            "AND status=1",
            "</if>",
            "<if test='agreement.status == 3'>",
            "AND status=2",
            "</if>",
            "</script>"
    })
    List<Agreement> updateByAgreement(@Param("agreement") Agreement agreement);


    @Select("SELECT u.* " +
            "FROM t_user u " +
            "JOIN t_agreement a ON u.id = a.user_in " +
            "WHERE a.user_out = #{agreement.userOut} " +
            "  AND a.goods_id = #{agreement.goodsId} " +
            "  AND a.status = 1")
    User selectUserByAgreement(@Param("agreement") Agreement agreement);


    @Select({"SELECT a.*, ",
            "g.id as goods_id, ",
            "g.name as name, ",
            "g.price as price, ",
            "g.unit as unit, ",
            "g.img as img, ",
            "g.remark as remark ",
            "FROM t_agreement a ",
            "LEFT JOIN t_goods g ON a.goods_id = g.id ",
            "WHERE a.user_in = #{userId}"})
    Agreement selectAgreementInf(@Param("goods") Goods goods);


    @Insert("INSERT INTO t_agreement (goods_id, user_out,user_in,status) " +
            "VALUES (#{agreement.goodsId}, #{agreement.userOut}, #{agreement.userIn}, #{agreement.status})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int insertRTID(@Param("agreement") Agreement agreement);


    @Update({"UPDATE t_agreement",
            "SET status = 2",
            "WHERE id = #{id}"})
    void returnGoodsById(@Param("id") int id);

    @Select({
            "<script>",
            "SELECT a.*, ",
            "g.id as goods_id, ",
            "g.name as name, ",
            "g.price as price, ",
            "g.unit as unit, ",
            "g.img as img, ",
            "g.remark as remark, ",  // 添加逗号
            "uo.nickname as userOutName, ",  // 添加逗号
            "ui.nickname as userInName ",  // 最后一行的逗号可以省略
            "FROM t_agreement a ",
            "LEFT JOIN t_goods g ON a.goods_id = g.id ",
            "LEFT JOIN t_user ui ON a.user_in = ui.id ",  // 更正为 'ui'
            "LEFT JOIN t_user uo ON a.user_out = uo.id ",  // 更正为 'uo'
            "<if test='name != null'>",
            "AND g.name LIKE CONCAT('%', #{name}, '%')",  // 更正为 'g.name'
            "</if>",
            "</script>"
    })
    List<Agreement> selectListByName(String name);





}
